package com.hibegin.ftp;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FtpSession {

    private static final String PACKAGESPLIT = "\r\n";
    private FtpUser ftpUser;
    private FtpNIOClient ftpClient;
    private Map<String, Object> attr = new ConcurrentHashMap<String, Object>();

    public FtpSession(FtpUser ftpUser, FtpNIOClient ftpClient) {
        super();
        this.ftpUser = ftpUser;
        this.ftpClient = ftpClient;
    }

    public FtpUser getFtpUser() {
        return ftpUser;
    }

    public void setFtpUser(FtpUser ftpUser) {
        this.ftpUser = ftpUser;
    }

    public Map<String, Object> getAttr() {
        return attr;
    }

    public void setAttr(Map<String, Object> attr) {
        this.attr = attr;
    }

    public void sendMsg(String str) {
        SocketChannel client = ftpClient.getClient();
        SelectionKey selectionKey = ftpClient.getSelectionKey();
        try {
            if (str != null) {
                String tSendMsg = str + PACKAGESPLIT;
                client = (SocketChannel) selectionKey.channel();
                ByteBuffer sendbuffer = ByteBuffer
                        .allocate(tSendMsg.getBytes().length);
                sendbuffer.clear();
                sendbuffer.put(tSendMsg.getBytes());
                sendbuffer.flip();
                client.write(sendbuffer);
                System.out.print("req --> " + tSendMsg);
            }
            client.register(ftpClient.getSelector(), SelectionKey.OP_READ);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void readMsg(final String str) {
        if (str.substring(0, 4).contains(" ")) {
            final FtpSession ftpSession = this;
            new Thread() {
                public void run() {
                    Integer status = Integer.parseInt(str.substring(0, 3));
                    String tstr = str.substring(4);
                    Method method = StatusMap.getMethod(status);
                    if (method != null) {
                        String req;
                        try {
                            req = (String) method.invoke(method.getDeclaringClass().getConstructor(FtpSession.class).newInstance(ftpSession), tstr);
                            //ftpClient.getClient().register(ftpClient.getSelector(), SelectionKey.OP_WRITE,req);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("unsupport statusCode" + status);
                    }

                }
            }.start();
        }
    }

    public void close() {
        try {
            ftpClient.getClient().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
